import { defineStore } from 'pinia'
import request from '@/utils/request'
import { GoodsInfo, ApiRes, CityListType } from '@/types/data'
export default defineStore('goods', {
  state () {
    return {
      goodsInfo: {} as GoodsInfo, // 商品详情
      addressList: [] as CityListType[], // 城市列表渲染数据
      initAddressList: [] as CityListType[]
    }
  },
  actions: {
    // 商品详情
    async goods (id: string) {
      const res = await request.request<ApiRes<GoodsInfo>>({
        url: '/goods',
        params: { id }
      })
      this.goodsInfo = res.data.result
      console.log('商品详情', res)
    },
    // 获取地址数据
    async getCityList () {
      const res = await request.request<CityListType[]>({
        url:
          'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
        // 基地址：特性，如果以http开头完整地址它不使用基地址，不是完整地址时才使用基地址
      })
      this.addressList = res.data
      this.initAddressList = res.data // 存储初始数据
      // console.log(res)
    },
    // 修改城市列表数据
    setAddressList (list: CityListType[]) {
      this.addressList = list
    }
  }
})
